package com.yonyou.pmclouds.repeatwork.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.IllegalRequestArgException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.repeatwork.entity.RepeatWorkVO;
import com.yonyou.pmclouds.repeatwork.service.rmiitf.IRepeatWorkMaintain;
import com.yonyou.pmclouds.repeatwork.service.rmiitf.IRepeatWorkQuery;
import com.yonyou.pmclouds.safetyrectification.entity.SafetyRectificationVO;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 复工单
 */
@RestController
@RequestMapping({"mobile/repeatWork","outter/repeatWork"})
public class RepeatWorkController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IRepeatWorkMaintain repeatWorkMaintain;
    @Reference(version=ApplicationConsts.APPLICATION_VERSION)
    private IRepeatWorkQuery repeatWorkQuery;

    /**
     * 保存复工单
     * @param vo
     * @param errors
     * @return
     * @throws BusinessException
     */
    @Insert
    @PostMapping("/add")
    @PMApprove
    public RepeatWorkVO add(@RequestBody @Validated RepeatWorkVO vo, BindingResult errors) throws BusinessException {
        if (errors.hasErrors())
            throw new IllegalRequestArgException();
        initInsertVO(vo);
        //智能日志需要制单人信息
        vo.setCreator(RuntimeEnvironment.getLoginUserId());
        RepeatWorkVO resultVO = repeatWorkMaintain.add(vo);
        return resultVO;
    }

    /**
     * 修改复工单
     * @param vo
     * @param errors
     * @return
     * @throws BusinessException
     */
    @PostMapping("/update")
    @PMApprove(trigger = "commit")
    public RepeatWorkVO update(@RequestBody @Validated RepeatWorkVO vo, BindingResult errors) throws  BusinessException{
        if (errors.hasErrors())
            throw new IllegalRequestArgException();

        RepeatWorkVO resultVO = repeatWorkMaintain.update(vo);

        return resultVO;
    }

    /**
     * 查看复工单详细信息
     * @param pkRepeatWork
     * @return
     * @throws BusinessException
     */
    @RequestMapping("/getRepeatWorkDetail")
    public RepeatWorkVO getRepeatWorkDetail(String pkRepeatWork) throws BusinessException{
        RepeatWorkVO repeatWorkVO = repeatWorkQuery.queryOneByPk(pkRepeatWork);
        return repeatWorkVO;
    }

    private void initInsertVO(RepeatWorkVO vo) {
        if (vo == null) return;
        vo.setBillType(BillTypeConst.REPEATWORK_BILLTYPE);
        vo.setBillStatus(BillStatusConst.COMMON_FREE_STATUS);
    }
}
